home *** CD-ROM | disk | FTP | other *** search
- Path: news.ner.bbnplanet.net!forest!groy
- From: groy@forest.drew.edu (I can't think of a nickname)
- Newsgroups: comp.lang.c
- Subject: HELPP!!! --very important-- #2
- Message-ID: <1996Mar21.001307.138164@forest>
- Date: 21 Mar 96 00:13:07 EST
- Organization: Drew University
-
- Hello once again fellow programmers.. here is another program problem..
- It's supposed to print out the prime numbers from 1 to 1000..(I took this
- from a book) but multiples of 3,and 5 keep comming up.. help??
-
-
- #include <stdio.h>
- #include <math.h>
- #define NMAX 1000
-
-
-
- int main (void)
-
-
- int n = 1,
- min_div, /*minimum divisor (greater than 1) of n */
- step = 1;
- printf ("%i ",n); /* prints out 1.. thew first prime number */
- for(n=2; n <= NMAX; n++)
- min_div = find_div(n);
- if (min_div == n)
- printf("%i ", n); step++;
- if (step == 20)
- printf("\n"); step =0; /*end of if min_div == n */
- /* end for loop */
- printf("\n"); /*nesisary carriage return (sorry about my spelling) */
- /* end main */
- int find_div(int n)
-
- int trial, /* current value of n */
- divisor; /* smallest divisor of n; zero means that divisor not
- yet found */
- int even(int num);
-
- if (even(n))
- divisor = 2;
- else
- divisor =0;
- trial = 3;
-
- while (divisor == 0) /* finds is there is any possible int that
- would kepp it from being a prime number */
- if (trial > sqrt(n))
- divisor = n;
- else if (( n % trial) == 0)
- divisor = trial;
- else
- trial = trial + 2;
-
-
- return (divisor);
- /* end find_div */
-
- int even(int num) /* finds is num is even or not */
- int ans;
-
- ans = ((num % 2) == 0);
- return (ans); /*returns 1 if even, 0 if not */
-
- /* end even */
-
-
- Sorry about the formatting... the \ symbol is a backslash... for some
- reason my comp doesn't wabt to read in the backslash.. :(
-
-
- Greg Roy
- Groy@drew.edu
-